package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabMaininfoXsGroup;
import com.ysd.lis.mapper.lab.LabMaininfoXsGroupMapper;
import com.ysd.lis.request.AddAndUpdateCancleauditGroup;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.service.lab.LabMaininfoXsGroupService;
import com.ysd.util.IncrementalNumberGenerator;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 不合格原因设置表 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2024-02-02
 */
@Service
public class LabMaininfoXsGroupServiceImpl extends ServiceImpl<LabMaininfoXsGroupMapper, LabMaininfoXsGroup> implements LabMaininfoXsGroupService {

    @Autowired
    LabMaininfoXsGroupMapper labMaininfoXsGroupMapper;
    @Override
    public Result getCancleauditList(LabMaininfoXsGroup labUnqualifiedGroup) {
        MPJLambdaWrapper<LabMaininfoXsGroup> queryWrapper = new MPJLambdaWrapper<>();
//        queryWrapper.selectAll(LabMaininfoXsGroup.class);
//        queryWrapper.selectAs("lug", LabUnqualifiedReason::getReason,LabMaininfoXsGroup::getReason);
//        queryWrapper.leftJoin(LabUnqualifiedReason.class,"lug", LabUnqualifiedReason::getId,LabMaininfoXsGroup::getUnqualifiedReasonId);
        queryWrapper.eq(LabMaininfoXsGroup::getDelFlag, 0);
        SysUser sysUser = RedisUserManager.getUser();
        if(ToolsUtils.isNotEmpty(sysUser)){
            queryWrapper.eq(LabMaininfoXsGroup::getOrgId, sysUser.getOrgId());
        }
         if(ToolsUtils.isNotEmpty(labUnqualifiedGroup)){
             if(ToolsUtils.isNotEmpty(labUnqualifiedGroup.getKeywords())){
                 queryWrapper.like(LabMaininfoXsGroup::getCancleauditGroup, labUnqualifiedGroup.getKeywords());
             }
         }
        queryWrapper.orderByAsc(LabMaininfoXsGroup::getSeq);
        List<LabMaininfoXsGroup> list =  labMaininfoXsGroupMapper.selectList(queryWrapper);
        return Result.succ(list);
    }

    @Override
    public Result addGroup(AddAndUpdateCancleauditGroup addAndUpdateCancleauditGroup) {
        if(ToolsUtils.isEmpty(addAndUpdateCancleauditGroup)){
            return Result.fail("入参不可为空");
        }
        List<LabMaininfoXsGroup> unqualifiedGroups = addAndUpdateCancleauditGroup.getCancleauditGroups();
        SysUser sysUser = RedisUserManager.getUser();
        for (LabMaininfoXsGroup unqualifiedGroup : unqualifiedGroups) {
            unqualifiedGroup.setOrgId(sysUser.getOrgId());
            unqualifiedGroup.setGroupCode(IncrementalNumberGenerator.generateIncrementalNumber6());
        }
        boolean save = this.saveBatch(unqualifiedGroups);
        if(save){
            return Result.fail("新增成功");
        }else {
            return Result.fail("新增失败");
        }
    }

    @Override
    public Result updateGroup(AddAndUpdateCancleauditGroup labUnqualifiedGroup) {
        if(ToolsUtils.isEmpty(labUnqualifiedGroup)){
            return Result.fail("入参不可为空");
        }
        List<LabMaininfoXsGroup> unqualifiedGroups = labUnqualifiedGroup.getCancleauditGroups();
        boolean save = this.updateBatchById(unqualifiedGroups);
        if(save){
            return Result.fail("修改成功");
        }else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result daleteGroup(DeleteParam deleteParam) {
        List<String> ids =  Arrays.asList(deleteParam.getIds().split(","));
        Boolean delete = this.removeByIds(ids);
        if(delete){
            return Result.fail("删除成功");
        }else {
            return Result.fail("删除失败");
        }
    }
}
